package com.ltu.mapper;

import com.ltu.domain.mp_entity.OrderEntity;
import com.ltu.domain.mp_entity.UserEntity;
import com.ltu.model.response.excel.OrderRecordVO;
import com.ltu.model.response.order.OrderVO;
import com.ltu.model.response.statistic.OrderStatisticVO;
import com.ltu.vo.XcxUserOrderCountVO;


import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * <p>
 * 设备续费订单 Mapper 接口
 * </p>
 * @author 若尘
 * @since 2022-02-21
 */
public interface OrderMapper extends BaseMapper<OrderEntity> {
	


	
	@Select(value = " select o.* ,d.product_id,d.product_name,d.banner_img,unit_price,unit_name  from sys_order o  LEFT JOIN  sys_order_detail  d on d.order_id = o.id  ${ew.customSqlSegment}")
	Page<OrderVO>   customisePage(Page<XcxUserOrderCountVO> page,@Param(Constants.WRAPPER) Wrapper<OrderEntity>  wrapper);
	
	@Select(value = " select o.id,o.store_name,o.user_name,d.product_id,d.product_name,unit_name, ROUND(unit_price/100,2) as unitPrice, "
			+ "d.amount,ROUND( o.payable_fee/100,2) as payable_fee,o.delivery_status,o.create_time,o.update_time, o.hexiao_date  ,s.address,s.telephone  "
			+ " from sys_order o  LEFT JOIN  sys_order_detail  d on d.order_id = o.id  "
			+ " LEFT JOIN t_store  s  on  s.id = o.store_id "
			+ " ${ew.customSqlSegment} ")
	List<OrderRecordVO>   customiseExport(@Param(Constants.WRAPPER) Wrapper<OrderEntity>  wrapper); 

    @Select(value = "SELECT  COUNT(*) dthOrder,        \r\n"
            + "             sum(amount) dthAmount     "
            + "              from sys_order  ${ew.customSqlSegment} ")
    XcxUserOrderCountVO   xcxUserOrderCountAll(@Param(Constants.WRAPPER) Wrapper<OrderEntity>  wrapper);

	
	
	
	@Select(value = " SELECT  o.store_name as storeName,d.product_name as  productName, COUNT(DISTINCT o.id) as  orderNumber,SUM( d.amount) as productNumber,SUM( o.payable_fee) as  totalMoney from  sys_order  o \r\n"
			+ "	LEFT JOIN sys_order_detail  d  on d.order_id = o.id \r\n"
			+ "	${ew.customSqlSegment} \r\n"
			+ "	GROUP  BY storeName,productName \r\n"
			+ "	ORDER BY orderNumber DESC ")
	Page<OrderStatisticVO>   countByStoreOrdersPage(Page<OrderStatisticVO> page,@Param(Constants.WRAPPER) Wrapper<OrderEntity>  wrapper);
	
	@Select(value =  " SELECT   COUNT(DISTINCT o.id) as  orderNumber,SUM( o.amount) as productNumber,SUM( o.payable_fee) as  totalMoney from  sys_order  o  "
			+ " LEFT JOIN sys_order_detail  d  on d.order_id = o.id  ${ew.customSqlSegment}")
	OrderStatisticVO   countByStoreOrdersTotal(@Param(Constants.WRAPPER) Wrapper<OrderEntity>  wrapper);
	
	
	@Select(value = " SELECT  o.store_name as storeName,d.product_name as  productName,s.address ,  s.telephone, COUNT(DISTINCT o.id) as  orderNumber,SUM( d.amount) as productNumber,SUM( o.payable_fee) as  totalMoney from  sys_order  o \r\n"
			+ "	LEFT JOIN sys_order_detail  d  on d.order_id = o.id \r\n"
			+ "	LEFT JOIN t_store  s  on s.id = o.store_id \r\n"
			+ "	${ew.customSqlSegment} \r\n"
			+ "	GROUP  BY storeName,productName \r\n"
			+ "	ORDER BY orderNumber DESC ")
	List<OrderStatisticVO>   countByStoreOrders(@Param(Constants.WRAPPER) Wrapper<OrderEntity>  wrapper);
	
	@Select(value = " select o.* ,d.product_id,d.product_name,d.banner_img,unit_price,unit_name  from sys_order o  LEFT JOIN  sys_order_detail  d on d.order_id = o.id  ${ew.customSqlSegment}")
	List<OrderVO>   getOrderDetailList(@Param(Constants.WRAPPER) Wrapper<OrderEntity>  wrapper);
	
	@Select(value = " select o.* ,d.product_id,d.product_name,d.banner_img,unit_price,unit_name  from sys_order o  LEFT JOIN  sys_order_detail  d on d.order_id = o.id  ${ew.customSqlSegment}  limit 1")
	OrderVO   getOneOrderDetail(@Param(Constants.WRAPPER) Wrapper<OrderEntity>  wrapper);
	
	


	
	
	@Select(value = "SELECT  user_id, sum(total_fee) as totalFee  FROM  sys_order  ${ew.customSqlSegment}  ")
	List<OrderEntity>  getOrderFensCount(@Param(Constants.WRAPPER) Wrapper<OrderEntity>  wrapper);
	
	@Select(value = "SELECT  user_id, sum(remain_time) as totalFee  FROM  t_user_in_room_record   ${ew.customSqlSegment}  ")
	List<OrderEntity>  getRoomEnvFensCount(@Param(Constants.WRAPPER) Wrapper<OrderEntity>  wrapper);
	
	
	/**
	 * + "WHERE pay_status = 1 and `status` = 1 and delivery_status > 1 and user_id = 63702 and o.company_code = 8 and product_id in (67,65)\r\n"
			+ "ORDER BY id desc   LIMIT 1; \r\n"
	 */
	@Select(value = "SELECT o.id,d.product_id,d.amount  from sys_order  o \r\n"
			+ "LEFT JOIN  sys_order_detail  d  on   d.order_id= o.id \r\n"
			+ "${ew.customSqlSegment}  ")
	OrderEntity  checkUserOrderLimit(@Param(Constants.WRAPPER) Wrapper<OrderEntity>  wrapper);
	
	
	
	
}
